package cn.hs.ph.dao.impl;

import cn.hs.ph.dao.ShopDao;
import cn.hs.ph.entity.Shop;
import cn.hs.ph.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class ShopDaoImpl implements ShopDao {
    @Override
    public int insert(Shop shop) {
        int num=0;
        String sql="insert into shop values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,null)";
        Object[] obj={
                shop.getShopName(),
                shop.getShopDescribe(),
                shop.getShopImage(),
                shop.getShopPhone(),
                shop.getShopProvince(),
                shop.getShopCity(),
                shop.getShopDistrict(),
                shop.getShopAddress(),
                shop.getShopLongitude(),
                shop.getShopLatitude(),
                1,
                new Date(),
                shop.getShopUserId(),
                shop.getShopAccount(),
                shop.getShopPassword(),
                shop.getShopSort(),
                shop.getSalesVolume(),
                shop.getDistance(),
                shop.getShopCommunitySort(),
                shop.getConcernStatus(),
                shop.getShopOrderCount(),
                shop.getShopCategoryList(),
                shop.getShopGoodsList(),
        };
        num = JDBCDruidUtil.update(sql,obj);
        return num;
    }

    //修改
    @Override
    public int update(Shop shop) {
        int num = 0;
        String sql = "update shop set addTime=?," +
                "salesVolume=?," +
                "shopAddress=?," +
                "shopCity=?," +
                "shopDescribe=?," +
                "shopDistrict=?," +
                "shopImage=?," +
                "shopLatitude=?," +
                "shopLongitude=?," +
                "shopName=?," +
                "shopOrderCount=?," +
                "shopPhone=?," +
                "shopProvince=?," +
                "shopStatus=?," +
                "shopUserId=?" +
                " where shopId=?";
        Object[] obj={
                shop.getAddTime(),
                shop.getSalesVolume(),
                shop.getShopAddress(),
                shop.getShopCity(),
                shop.getShopDescribe(),
                shop.getShopDistrict(),
                shop.getShopImage(),
                shop.getShopLatitude(),
                shop.getShopLongitude(),
                shop.getShopName(),
                shop.getShopOrderCount(),
                shop.getShopPhone(),
                shop.getShopProvince(),
                shop.getShopStatus(),
                shop.getShopUserId(),
                shop.getShopId()
        };
        num = JDBCDruidUtil.update(sql, obj);
        return num;
    }

    @Override
    public List<Shop> selectAll(Map<String, String> map) {
        List<Shop> list =new ArrayList<>();
        String sql ="select * from shop where 1=1";
        List<Object> params = new ArrayList<>();
        //门店名模糊查询
        String keyword = map.get("keyword");
        if(keyword!=null && !"".equals(keyword)){
            sql+=" and shopName like ?";
            params.add("%"+keyword+"%");
        }
        //手机号模糊查询
        String phone = map.get("phone");
        if(phone!=null && !"".equals(phone)){
            sql+=" and shopPhone like ?";
            params.add("%"+phone+"%");
        }
        //时间范围查询
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if(startTime!=null && !"".equals(startTime)){
            sql+=" and addTime between ? and ?";
            params.add(startTime);
            params.add(endTime);
        }
        //状态查询
        String status = map.get("shopStatus");

        if(status!=null && !"".equals(status)){
            int shopStatus = Integer.parseInt(status);
            sql+=" and shopStatus = ?";
            params.add(shopStatus);
        }
        //分页查询
        String pn = map.get("pageNum");
        if (pn != null && !"".equals(pn)) {
            sql += " limit ?, ? ";
            int pageNum = Integer.parseInt(pn);
            int pageSize = Integer.parseInt(map.get("pageSize"));
            params.add((pageNum - 1) * pageSize);
            params.add(pageSize);
        }
        //最终查询
        ResultSet res = JDBCDruidUtil.query(sql, params.toArray());
        while (true) {
            try {
                if (!res.next()) break;
                Shop shop = new Shop(res.getInt(1),
                        res.getString(2),
                        res.getString(3),
                        res.getString(4),
                        res.getString(5),
                        res.getString(6),
                        res.getString(7),
                        res.getString(8),
                        res.getString(9),
                        res.getString(10),
                        res.getString(11),
                        res.getInt(12),
                        res.getDate(13),
                        res.getInt(14),
                        res.getString(15),
                        res.getString(16),
                        res.getString(17),
                        res.getInt(18),
                        res.getString(19),
                        res.getString(20),
                        res.getString(21),
                        res.getInt(22),
                        res.getString(23),
                        res.getString(24));
                list.add(shop);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        JDBCDruidUtil.close();
        return list;
    }

    @Override
    public int selectCount(Map<String, String> map) {
        int count=0;
        String sql ="select count(1) from shop where 1=1";
        List<Object> params = new ArrayList<>();
        //门店名模糊查询
        String keyword = map.get("keyword");
        if(keyword!=null && !"".equals(keyword)){
            sql+=" and shopName like ?";
            params.add("%"+keyword+"%");
        }
        //手机号模糊查询
        String phone = map.get("phone");
        if(phone!=null && !"".equals(phone)){
            sql+=" and shopPhone like ?";
            params.add("%"+phone+"%");
        }
        //时间范围查询
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if(startTime!=null && !"".equals(startTime)){
            sql+=" and addTime between ? and ?";
            params.add(startTime);
            params.add(endTime);
        }
        //状态查询
        String status = map.get("shopStatus");
        if(status!=null && !"".equals(status)){
            int shopStatus = Integer.parseInt(status);
            sql+=" and shopStatus = ?";
            params.add(shopStatus);
        }
        //最终查询
        ResultSet res = JDBCDruidUtil.query(sql, params.toArray());
        while (true) {
            try {
                if (!res.next()) break;
                count=res.getInt(1);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        JDBCDruidUtil.close();
        return count;
    }
}
